package com.example.online_chat.api;

import com.example.online_chat.model.Message;
import com.example.online_chat.model.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
public class MessageAPI {
    @Autowired
    private MessageMapper messageMapper;

    @GetMapping("/message")
    public List<Message> getHistoryMessage(int sessionId) {
        // 得到的消息是倒序排列的, 要把这个 list 逆置一下变成正序的
        List<Message> historyMessage = messageMapper.getMessageBySessionId(sessionId);
        Collections.reverse(historyMessage);
        return historyMessage;
    }
}
